﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Script.Serialization;

namespace Meotom.Upload.v2.Data.BLL
{
    public class UploadsListView
    {
        public string UploadId { get; set; }
        public int UploadIdentifyId { get; set; }
        public string UploadName { get; set; }
        public DateTime CreateDate { get; set; }
        public bool Status { get; set; }
        public string UploadBy { get; set; }
        public string AddedByIp { get; set; }
    }
    public class Uploads
    {
        public static Uploads Instance
        {
            get { return new Uploads(); }
        }

        public List<UploadsListView> GetById(int imgId)
        {
            using (var dc = new mtDBUploadDataContext())
            {
                var query = from tblUpload in dc.tblUploads
                            where tblUpload.UploadIdentifyID == imgId
                            select new UploadsListView
                                       {
                                           UploadBy = tblUpload.UploadBy,
                                           UploadId = tblUpload.UploadID.ToString(),
                                           AddedByIp = tblUpload.AddedByIP,
                                           CreateDate = Convert.ToDateTime(tblUpload.CreateDate),
                                           Status = bool.Parse(Convert.ToString(tblUpload.Status)),
                                           UploadIdentifyId = tblUpload.UploadIdentifyID,
                                           UploadName = tblUpload.UploadName
                                       };
                return query.ToList();
            }
        }

        public List<UploadsListView> GetByUser(string userId)
        {
            using (var dc = new mtDBUploadDataContext())
            {
                var query = from tblUpload in dc.tblUploads
                            where tblUpload.UploadBy == userId
                            select new UploadsListView
                            {
                                UploadBy = tblUpload.UploadBy,
                                UploadId = tblUpload.UploadID.ToString(),
                                AddedByIp = tblUpload.AddedByIP,
                                CreateDate = Convert.ToDateTime(tblUpload.CreateDate),
                                Status = bool.Parse(Convert.ToString(tblUpload.Status)),
                                UploadIdentifyId = tblUpload.UploadIdentifyID,
                                UploadName = tblUpload.UploadName
                            };
                return query.ToList();
            }
        }

        public bool DeleteTemp(int uploadId, string uploadBy)
        {
            using (var dc = new mtDBUploadDataContext())
            {
                var query = (from tblUpload in dc.tblUploads
                            where tblUpload.UploadIdentifyID == uploadId && tblUpload.UploadBy == uploadBy
                            select tblUpload);

                if (!query.Any()) return false;
                if (!DeleteTempFile(query)) return false;

                dc.UpdateUpload(uploadId);
            }
            return true;
        }

        public bool Delete(string uploadId, string uploadBy)
        {
            using (var dc = new mtDBUploadDataContext())
            {
                var query = from tblUpload in dc.tblUploads
                            where tblUpload.UploadID == Guid.Parse(uploadId) && tblUpload.UploadBy == uploadBy
                            select tblUpload;

                if (!query.Any()) return false; 
                if (!DeleteImage(query)) return false;
                dc.tblUploads.DeleteAllOnSubmit(query);
                dc.SubmitChanges();
            }
            return true;
        }

        public bool Delete(int uploadIdentifyId, string uploadBy)
        {
            using (var dc = new mtDBUploadDataContext())
            {
                var query = from tblUpload in dc.tblUploads
                            where tblUpload.UploadIdentifyID == uploadIdentifyId && tblUpload.UploadBy == uploadBy
                            select tblUpload;

                if (!query.Any()) return false; 
                if (!DeleteImage(query)) return false;
                dc.tblUploads.DeleteAllOnSubmit(query);
                dc.SubmitChanges();
            }
            return true;
        }

        public bool DeleteTempFile(IQueryable<tblUpload> tblUploads)
        {
            try
            {
                foreach (var tblUpload in tblUploads)
                {
                    var strFullPath = Configs.UserFolder + Utilities.Common.Instance.SplitString(tblUpload.UploadBy)[3];
                    var arrImgSize = "475|130|100".Split(new[] { '|' });
                    var upload = tblUpload;
                    Directory.CreateDirectory(strFullPath + @"_deleted\");

                    foreach (var s in from s in arrImgSize let theFile = new FileInfo(strFullPath + @"\" + s + "x0__" + upload.UploadName) where theFile.Exists select s)
                    {
                        File.Move(strFullPath + @"\" + s + "x0__" + tblUpload.UploadName, strFullPath + @"_deleted\" + s + "x0__" + tblUpload.UploadName);
                    }
                    File.Move(strFullPath + @"\" + tblUpload.UploadName, strFullPath + @"_deleted\" + tblUpload.UploadName);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteImage(IQueryable<tblUpload> tblUploads)
        {
            try
            {
                foreach (var tblUpload in tblUploads)
                {
                    var strFullPath = Configs.UserFolder + Utilities.Common.Instance.SplitString(tblUpload.UploadBy)[3];
                    var arrImgSize = "475|130|100".Split(new[] { '|' });
                    var upload = tblUpload;
                    foreach (var s in from s in arrImgSize let theFile = new FileInfo(strFullPath + @"\" + s + "x0__" + upload.UploadName) where theFile.Exists select s)
                    {
                        File.Delete(strFullPath + @"\" + s + "x0__" + tblUpload.UploadName);
                    }
                    File.Delete(strFullPath + @"\" + tblUpload.UploadName);

                    Directory.Delete(strFullPath + @"_deleted\");
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public string GetImgJson(int imgId)
        {
            var s = new JavaScriptSerializer();
            return s.Serialize(GetById(imgId));
        }
    }
}